package com.yh.homestay.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.homestay.common.CapitalGoodsResult;
import com.yh.homestay.common.HomeStayResult;
import com.yh.homestay.dao.CapitalGoodsFindNameMapper;
import com.yh.homestay.dao.CapitalGoodsMapper;
import com.yh.homestay.pojo.CapitalGoods;
import com.yh.homestay.pojo.CapitalGoodsExample;
import com.yh.homestay.service.CapitalGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 物资管理服务
 */
@Service
public class CapitalGoodsServiceImpl implements CapitalGoodsService {
    @Autowired
    private CapitalGoodsMapper capitalGoodsMapper;//注入mapper

    @Autowired
    private CapitalGoodsFindNameMapper capitalGoodsFindNameMapper;

    //查询所有物资信息
    @Override
    public HomeStayResult findAllCapitalGoods(Integer page, Integer limit, String start, String end, String goodsName) {
        Map map=new HashMap();
        map.put("start",start);
        map.put("end",end);
        map.put("goodsName",goodsName);
        CapitalGoodsExample example=new CapitalGoodsExample();
        CapitalGoodsExample.Criteria criteria = example.createCriteria();
        PageHelper.startPage(page,limit);
        List<CapitalGoods> list=capitalGoodsFindNameMapper.listMatriels(map);
        PageInfo info=new PageInfo(list);
        return new HomeStayResult(0,"OK",(int) info.getTotal(),info.getList());

    }
    public void test(){

    }

    //添加物资
    @Override
    public CapitalGoodsResult addCapitalGood(CapitalGoods good) {
        int add=capitalGoodsMapper.insert(good);
        if (add > 1){
             return CapitalGoodsResult.ok();
        }else{
            return CapitalGoodsResult.build(500,"出错了");
        }
    }
    //删除物资
    @Override
    public CapitalGoodsResult delSingleCapitalGood(String goodsNumber) {

        int del=del(goodsNumber);//调用删除方法
       if (del>0){
           return CapitalGoodsResult.ok();
       }else{
          return CapitalGoodsResult.build(500,"出错了");
       }

    }
//批量删除物资
    @Override
    public CapitalGoodsResult delCapitalGood(String nums) {
        List<String> list=new ArrayList<String>();
        String[] split=nums.split(",");
        for (String string : split) {
            list.add(string);
        }
        int delete=0;
        for (int i=0;i<list.size();i++){
            del(list.get(i));//调用删除方法
            delete++;
        }
        if (delete>0){
            return CapitalGoodsResult.ok();
        }else{
            return CapitalGoodsResult.build(500,"出错了");
        }
    }
    //查询物资信息
    @Override
    public CapitalGoodsResult seleteCapitalSingle(String num) {
        CapitalGoods good=new CapitalGoods();
        good.setGoodsNumber(num);
       CapitalGoods cg=capitalGoodsMapper.selectOne(good);
       CapitalGoodsResult res=new CapitalGoodsResult(200,"success",cg);
       return res;
    }
//修改物资信息
    @Override
    public CapitalGoodsResult updataMaterielSingle(CapitalGoods good) {
        CapitalGoodsExample example=new CapitalGoodsExample();
        CapitalGoodsExample.Criteria criteria=example.createCriteria();
        criteria.andGoodsIdEqualTo(good.getGoodsId());
       int update= capitalGoodsMapper.updateByExampleSelective(good,example);
       if (update==1){

               return CapitalGoodsResult.ok();
           }else{
               return CapitalGoodsResult.build(500,"出错了");
           }
    }
//删除物资方法
    private int del(String goodsNumber){
        CapitalGoods good=new CapitalGoods();
        good.setGoodsNumber(goodsNumber);
         return capitalGoodsMapper.delete(good);
    }

}
